<?php

function addComment($BDD_connexion,$id_bien,$titre_avis,$commentaire,$notation)
{
	$maxLengthTitre = 255;
	
	$requestUpdate = "INSERT INTO avis_utilisateur(titre_avis,commentaire,notation,timestamp,id_personne,id_bien) VALUES(:titre,:com,:note,:now,:user,:bien)";
	$prepareUpdate = $BDD_connexion->prepare($requestUpdate);
	$prepareUpdate->bindParam(':bien',$id_bien,PDO::PARAM_INT);
	$prepareUpdate->bindParam(':user',$_SESSION['userid'],PDO::PARAM_INT);
	$prepareUpdate->bindParam(':titre',$titre_avis,PDO::PARAM_STR,$maxLengthTitre);
	$prepareUpdate->bindParam(':com',nl2br($commentaire),PDO::PARAM_STR);
	$prepareUpdate->bindValue(':note',$notation,PDO::PARAM_INT);
	$prepareUpdate->bindValue(':now',time(),PDO::PARAM_INT);
	return $prepareUpdate->execute();
	
	return $BDD_connexion->lastInsertId();
	
}

function updateCommentContent($BDD_connexion,$id_com,$titre_avis,$commentaire,$notation)
{
	$maxLengthTitre = 255;
	
	$requestUpdate = "UPDATE avis_utilisateur SET titre_avis = :titre, commentaire = :com, notation = :note, authorized = 0, last_updated = :now WHERE id_avis = :id";
	$prepareUpdate = $BDD_connexion->prepare($requestUpdate);
	$prepareUpdate->bindValue(':id',$id_com,PDO::PARAM_INT);
	$prepareUpdate->bindParam(':titre',$titre_avis,PDO::PARAM_STR,$maxLengthTitre);
	$prepareUpdate->bindParam(':com',nl2br($commentaire),PDO::PARAM_STR);
	$prepareUpdate->bindValue(':note',$notation,PDO::PARAM_INT);
	$prepareUpdate->bindValue(':now',time(),PDO::PARAM_INT);
	
	return $prepareUpdate->execute();
	
}

function setAuthorisationComment($BDD_connexion,$id_com,$authorized)
{
	
	$requestAuth = "UPDATE avis_utilisateur SET authorized = :auth WHERE id_avis = :id";
	$prepareAuth = $BDD_connexion->prepare($requestAuth);
	$auth = $authorized ? 1 : 0;
	$prepareAuth->bindValue(':auth',$auth,PDO::PARAM_INT);
	$prepareAuth->bindValue(':id',$id_com,PDO::PARAM_INT);
	
	return $prepareAuth->execute();
	
}

function deleteCommentById($BDD_connexion,$id_com)
{
	
	$requestDelete = "DELETE FROM avis_utilisateur WHERE id_avis = :id_avis";
	$prepareDelete = $BDD_connexion->prepare($requestDelete);
	$prepareDelete->bindValue(':id_avis',$id_com,PDO::PARAM_INT);
	
	return $prepareDelete->execute();
	
}

function isReallyForGood($BDD_connexion,$id_com,$id_bien)
{
	
	$request = "SELECT count(*) AS count FROM avis_utilisateur WHERE id_avis = :id_avis AND id_bien = :id_bien";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindValue(':id_avis',$id_com,PDO::PARAM_INT);
	$prepare->bindValue(':id_bien',$id_bien,PDO::PARAM_INT);
	$prepare->execute();
	$count = $prepare->fetch(PDO::FETCH_OBJ);
	
	if (isset($count->count))
	{
		return $count->count;
	}
	else
	{
		return -1;
	}
	
}

function isReallyUserComment($locations,$id_good,$id_comment)
{
	
	$return = false;
	
	foreach ($locations as $location)
	{
		
		if (isset($location->commentaire->id_avis) && $location->bien->id_bien == $id_good && $location->commentaire->id_avis == $id_comment)
		{
			$return = true;
		}
		
	}
	
	return $return;
	
}

function getComDetails($BDD_connexion,$id_com)
{
	
	$requestCom = "SELECT * FROM avis_utilisateur WHERE id_avis = :id_avis";
	$prepareCom = $BDD_connexion->prepare($requestCom);
	$prepareCom->bindValue(':id_avis',$id_com,PDO::PARAM_INT);
	$prepareCom->execute();
	$advice = $prepareCom->fetch(PDO::FETCH_OBJ);
	
	return $advice;
	
}

function GetLocationAdvices($BDD_connexion, $id) {

	$advices = array();

	$request_count = "SELECT * FROM avis_utilisateur WHERE id_bien = :id";
	$prepare=$BDD_connexion->prepare($request_count);
	$prepare->bindValue(':id',$id,PDO::PARAM_INT);
	$prepare->execute();
	while ($advice = $prepare->fetch(PDO::FETCH_OBJ))
	{
		$request_user = "SELECT nom,prenom FROM personne WHERE personne.id = :id_personne";
		$prepare_user=$BDD_connexion->prepare($request_user);
		$prepare_user->bindValue(':id_personne',$advice->id_personne,PDO::PARAM_INT);
		$prepare_user->execute();
		$user = $prepare_user->fetch(PDO::FETCH_OBJ);
		$advice->utilisateur = $user;
		$advices[] = $advice;
	}
	
	return $advices;

}

function hasNoPreviousCommentFromUser($locations,$id_good)
{
	
	foreach ($locations as $location)
	{
		
		if (isset($location->commentaire->id_avis) && $location->bien->id_bien == $id_good)
		{
			return false;
		}
		
	}
	
	return true;
	
}

?>
